﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using Sicurezza.Entita;

namespace Sicurezza.CA.DataProvider
{
    public class RevokeDataProvider
    {

        public void Save(string serial, string reason)
        {

            using (OleDbConnection conn = new OleDbConnection(ConnectionStringDataProvider.ConnectionString))
            {
                string query = "INSERT INTO Revokes " +
                               "([Timestamp],Serial,Reason) " +
                               "VALUES (#" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "#," + serial + ",'" + reason + "')";

                OleDbCommand cmd = new OleDbCommand(query, conn);
                conn.Open();
                cmd.ExecuteNonQuery();

            }


        }

        public void Save(string serial)
        {
            Save(serial, "");
        }

        public List<RevokedCertificate> GetAll()
        {
            List<RevokedCertificate> list = new List<RevokedCertificate>();
            using (OleDbConnection conn = new OleDbConnection(ConnectionStringDataProvider.ConnectionString))
            {
                string query = "SELECT * " +
                               "FROM Revokes ";

                OleDbCommand cmd = new OleDbCommand(query, conn);
                conn.Open();
                OleDbDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    RevokedCertificate rc = new RevokedCertificate();
                    rc.CertificateSerial = Convert.ToInt32(dr["Serial"]);
                    rc.Timestamp = Convert.ToDateTime(dr["Timestamp"]);
                    list.Add(rc);
                }

            }

            return list;
        }
    }
}
